package com.yuanduo_app.utils.update;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.Gravity;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.xujl.fastlib.utils.AppManager;
import com.xujl.task.RxExecutor;
import com.xujl.task.RxHelper;

import com.yuanduo_app.R;
import com.yuanduo_app.consts.BundleKey;
import com.yuanduo_app.entity.UpdateInfo;
import com.yuanduo_app.service.UpdateService;
import com.yuanduo_app.window.BaseWindow;

public class UpdateInfoWindow extends BaseWindow implements View.OnClickListener {
    private TextView mInfoTv;
    private TextView mCancelTv;
    private TextView mConfirmTv;
    private View mBtnLayout;
    private View mline;
    private ProgressBar mProgressView;
    private TextView mProgressTv;
    private VersionHelper mVersionHelper;
    private boolean isForce;
    private UpdateInfo data;
    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive (Context context, Intent intent) {
            if (mProgressTv == null) {
                return;
            }
            mBtnLayout.setVisibility(View.INVISIBLE);
            mline.setVisibility(View.INVISIBLE);
            mProgressTv.setVisibility(View.VISIBLE);
            mProgressView.setVisibility(View.VISIBLE);
            int progress = intent.getIntExtra(BundleKey.DATA, 0);
            mProgressView.setProgress(progress);
            mProgressTv.setText("已下载 " + progress + "%");
            if (progress == 100) {
                mProgressTv.setText("安装应用");
                mProgressTv.setOnClickListener(v -> UpdateService.install(context));
            }
        }
    };

    public UpdateInfoWindow (Context context, VersionHelper helper) {
        super(context);
        mVersionHelper = helper;
        setPopupGravity(Gravity.CENTER);
        setOutSideDismiss(false);
        try {
            context.registerReceiver(mBroadcastReceiver, new IntentFilter(UpdateService.UPDATE_ACTION));
        } catch (Exception e) {
            e.printStackTrace();
        }
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss () {
                if (isForce) {
                    RxExecutor.getInstance()
                            .executeTask(new RxHelper.DelayTask(500) {
                                @Override
                                public void timeOver () {
                                    showPopupWindow();
                                }
                            });
                    AppManager.getAppManager().goSystemHome();
                } else {
                    try {
                        context.unregisterReceiver(mBroadcastReceiver);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }

    @Override
    public View onCreateContentView () {
        final View view = createPopupById(R.layout.window_update_info);
        mProgressView = view.findViewById(R.id.v_progress);
        mline = view.findViewById(R.id.view2);
        mProgressTv = view.findViewById(R.id.tv_progress);
        mBtnLayout = view.findViewById(R.id.ll_btn);
        mInfoTv = view.findViewById(R.id.tv_info);
        mConfirmTv = view.findViewById(R.id.tv_confirm);
        mConfirmTv.setOnClickListener(this);
        mCancelTv = view.findViewById(R.id.tv_cancel);
        mCancelTv.setOnClickListener(this);
        return view;
    }

    @Override
    public void onClick (View view) {
        if (!isForce) {
            dismiss();
        }
        switch (view.getId()) {
            case R.id.tv_confirm:
                mVersionHelper.getVersionCheckCallback().onVersionPass();
                mVersionHelper.startDownloadService();
                break;
            case R.id.tv_cancel:
                if (isForce) {
                    mVersionHelper.getVersionCheckCallback().onVersionRefuse();
                    return;
                }
                mVersionHelper.getVersionCheckCallback().onVersionPass();
                break;
            default:

                break;

        }
    }

    public void setData (UpdateInfo data) {
        this.data = data;
        isForce = data.isForce();
    }

    public void showPopupWindow () {
        super.showPopupWindow();
        mInfoTv.setText(data.getDeployNote());
        if (isForce) {
            mCancelTv.setText("退出应用");
//            mCancelTv.setOnClickListener(v -> AppManager.getAppManager().goSystemHome());
            mConfirmTv.setOnClickListener(v -> {
                mInfoTv.setText("请稍后，更新进行中...");
                mBtnLayout.setVisibility(View.INVISIBLE);
                mline.setVisibility(View.INVISIBLE);
                mProgressTv.setVisibility(View.VISIBLE);
                mProgressView.setVisibility(View.VISIBLE);
                mVersionHelper.startDownloadService();
            });
        }
    }

}
